package yns.springboot.frontend.sample.controller;

import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import tools.jackson.databind.ObjectMapper;

import yns.springboot.frontend.sample.base.web.BaseWeb;
import yns.springboot.frontend.sample.dto.WebSampleInfoDto;
import yns.springboot.frontend.sample.dto.layui.LayUIResponseInfoDto;
import yns.springboot.frontend.sample.service.LayuiSampleService;

@RestController
public class LayuiSampleController extends BaseWeb {
	
	// http://localhost:8080/layuiSample
	
	private final Logger logger = LoggerFactory.getLogger(this.getClass());
	
	private static List<WebSampleInfoDto> cacheList; 
	
	@Autowired
	private ObjectMapper objectMapper;
	
	@Autowired
	private LayuiSampleService layuiSampleService;
	
	@GetMapping("/layuiSample")
	public String layuiSample(@RequestParam("page") int page, @RequestParam("limit") int limit) throws IOException {
		logger.info(httpServletRequest.getRequestURI());
		logger.info("page : " + page + " limit : " + limit);
		
		List<WebSampleInfoDto> webSampleInfoDtos;
		if (cacheList == null) {
			webSampleInfoDtos = layuiSampleService.getSampleInfo();
			cacheList = webSampleInfoDtos;
			System.out.println("---------cacheList----------");
		} else {
			webSampleInfoDtos = cacheList;
			System.out.println("--------- use cacheList----------");
		}
		
		List<WebSampleInfoDto> pageList; 
		if (page > 1) {
			long skip = (page - 1) * limit;
			pageList = webSampleInfoDtos.stream().skip(skip).limit(limit).collect(Collectors.toList());
		} else {
			pageList = webSampleInfoDtos.stream().limit(limit).collect(Collectors.toList());
		}
		

		String listJson = objectMapper.writeValueAsString(webSampleInfoDtos);
		System.out.println(listJson);

		LayUIResponseInfoDto responseInfoDto = new LayUIResponseInfoDto();
		responseInfoDto.setCode(0);
		responseInfoDto.setCount(webSampleInfoDtos.size());
		responseInfoDto.setData(pageList);

		String responseInfoDtoJson = objectMapper.writeValueAsString(responseInfoDto);
		System.out.println(responseInfoDtoJson);
		
		return responseInfoDtoJson;
	}
	
	
	@GetMapping("/layuiSampleSearch")
	public String layuiSampleSearch(@RequestParam("page") int page,
			@RequestParam("limit") int limit,
			@RequestParam(value = "searchName",required = false) String searchName, 
			@RequestParam(value = "searchHref",required = false) String searchHref, 
			@RequestParam(value = "testDate",required = false) String testDate)
			throws IOException {
		logger.info(httpServletRequest.getRequestURI());
		logger.info("page : " + page + " limit : " + limit);
		logger.info("searchName : " + searchName + " searchHref : " + searchHref + " testDate : " + testDate);
		
		List<WebSampleInfoDto> webSampleInfoDtos;
		if (cacheList == null) {
			webSampleInfoDtos = layuiSampleService.getSampleInfo();
			cacheList = webSampleInfoDtos;
			System.out.println("---------cacheList----------");
		} else {
			webSampleInfoDtos = cacheList;
			System.out.println("--------- use cacheList----------");
		}
		
		List<WebSampleInfoDto> pageList; 
		if (page > 1) {
			long skip = (page - 1) * limit;
			pageList = webSampleInfoDtos.stream().skip(skip).limit(limit).collect(Collectors.toList());
		} else {
			pageList = webSampleInfoDtos.stream().limit(limit).collect(Collectors.toList());
		}
		
		String listJson = objectMapper.writeValueAsString(webSampleInfoDtos);
		System.out.println(listJson);
		
		if (searchName != null && !"".equals(searchName)) {
			WebSampleInfoDto webSampleInfoDto = pageList.get(0); 
			webSampleInfoDto.setType(searchName);
			pageList.set(0, webSampleInfoDto);
		}

		LayUIResponseInfoDto responseInfoDto = new LayUIResponseInfoDto();
		responseInfoDto.setCode(0);
		responseInfoDto.setCount(webSampleInfoDtos.size());
		responseInfoDto.setData(pageList);

		String responseInfoDtoJson = objectMapper.writeValueAsString(responseInfoDto);
		System.out.println(responseInfoDtoJson);
		
		return responseInfoDtoJson;
	}
	
}
